import {Injectable} from '@angular/core';
import {RequestUtil} from "../../../../util/request-util";
import {FormUtil} from "../../../../util/form-util";
import {Observable} from "rxjs";
import {MsgResult} from "../../../../models/msg-result";
import {Channel} from "./channel";
import {MsgAPI} from '../../msg-constants';

@Injectable()
export class MsgChannelService {

  constructor(
    private request: RequestUtil,
    private formUtil: FormUtil
  ) {
  }

  //添加通道
  addChannel(channel: Channel): Observable<MsgResult> {
    let params = "alarmName="+channel.alarmName;
    params += "&receiver="+channel.receiver;
    params += "&receiverName="+channel.receiverName;
    params += "&type="+channel.type;
    params += "&sendMode="+channel.sendMode;
    params += "&level="+channel.level;
    if(channel.isReal == '0'){
      params +=  "&isReal="+channel.isReal;
    }else{
      params +=  "&isReal="+ '1';
    }
    params += "&startTime="+channel.startTime;
    params += "&endTime="+channel.endTime;

    params += "&isUse="+channel.isUse;
    return this.request.post(MsgAPI.insertChannel, params);
  }

  //删除通道
  deleteChannel(id: string): Observable<MsgResult> {
    return this.request.delete(MsgAPI.deleteChannel + "&id=" + id);
  }

  //批量删除
  deleteChannels(ids: Array<String>): Observable<MsgResult> {
    let params = "idStrs=" + ids;
    return this.request.post(MsgAPI.delChannels, params);
  }

  //根据id获得通道
  getChannelById(id: string): Observable<MsgResult> {
    return this.request.get(MsgAPI.getChannelById + "&id=" + id);
  }

  //修改通道信息
  updateChannel(channel: Channel): Observable<MsgResult> {
    let params = "id="+channel.id;
    params +="&alarmName="+channel.alarmName;
    params += "&receiver="+channel.receiver;
    params += "&receiverName="+channel.receiverName;
    params += "&type="+channel.type;
    params += "&sendMode="+channel.sendMode;
    params += "&level="+channel.level;
    if(channel.isReal == '0'){
      params +=  "&isReal="+channel.isReal;
    }else{
      params +=  "&isReal="+ '1';
    }
    params += "&startTime="+channel.startTime;
    params += "&endTime="+channel.endTime;
    params += "&isUse="+channel.isUse;
    return this.request.put(MsgAPI.updateChannel, params);
  }

  //无参获取所有记录
  getChannel(): Observable<MsgResult> {
    return this.request.get(MsgAPI.getChannel);
  }


  getAllChannel():Observable<MsgResult>{
    return this.request.get(MsgAPI.getAllChannel);
  }

}
